
export const startScan = () => {
  return new Promise<any[]>((resolve, reject) => {
    uni.openBluetoothAdapter({
      success: () => {
        uni.startBluetoothDevicesDiscovery({
          success: (res) => {
            uni.onBluetoothDeviceFound((devices) => {
              const foundDevices = devices.devices.filter((device: any) => device.name);
              resolve(foundDevices);
            });
          },
          fail: (err) => reject(err),
        });
      },
      fail: (err) => reject(err),
    });
  });
};

export const connectDevice = (device: any) => {
  return new Promise<void>((resolve, reject) => {
    uni.createBLEConnection({
      deviceId: device.deviceId,
      success: () => resolve(),
      fail: (err) => reject(err),
    });
  });
};

export const disconnectDevice = (device: any) => {
  return new Promise<void>((resolve, reject) => {
    uni.closeBLEConnection({
      deviceId: device.deviceId,
      success: () => resolve(),
      fail: (err) => reject(err),
    });
  });
};